
package com.ruipengkj.view.orderView;
/**
 * 说明：
 * 添加礼品信息界面
 * @Author：孙航
 */
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TOrderRefund;
import com.ruipengkj.bean.TUser;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TOrderRefundService;
import com.ruipengkj.service.TUserService;
import com.ruipengkj.view.base.BaseDialog;


public class OrderRefundDialog extends BaseDialog {

	private static final long serialVersionUID = 1L;

	private JLabelJTextFieldComponent refundID;
	private JLabelJTextFieldComponent TOrder;
	private JLabelJTextFieldComponent happenTime;
	private JLabelJTextFieldComponent reason;
	private JLabelJComboBoxComponent status;
	private JLabelJTextFieldComponent replyTime;
	private JLabelJTextFieldComponent TUser;
	
	private JButton agreeButton;
	private JButton disagreeButton;
	private JButton cancelButton;
	
	private TOrderRefund tOrderRefund = null;
	private TOrderRefundService tOrderRefundService;
	
	private TUser tUser = null;
	private TUserService tUserService;
	
	Date date = new Date();
	public boolean isSuccess=false;//判断是否添加成功
	public OrderRefundDialog(JFrame jf,String title,boolean b) {
		super(jf, title, b);
		tOrderRefundService=BuildSpringApplication.getInstance().getTOrderRefundServiceImpl();
		tUserService=BuildSpringApplication.getInstance().getTUserServiceImpl();
		this.setSize(350, 500);
		this.init();
		this.initListener();
		this.setLocationRelativeTo(null);
	}
	
	public void init() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp,BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		
		refundID= new JLabelJTextFieldComponent("  *", "退款编号：", 30);
		TOrder = new JLabelJTextFieldComponent("  *", "订单编号：", 30);
		happenTime = new JLabelJTextFieldComponent("申请时间", 30);
		reason = new JLabelJTextFieldComponent("  *", "申请原因：", 30);
//		manager = new JLabelJComboBoxComponent("*", "仓库管理员：",new String[]{},188);
//		manager.addElement(new IDNameBean("0", "请选择"));
		status = new JLabelJComboBoxComponent("退款单状态：", ApplicationContext.ORDERREFUND_STATUS_MAP(), 100);
		replyTime = new JLabelJTextFieldComponent("商家操作时间", 30);
		TUser = new JLabelJTextFieldComponent("*", "用户名：", 30);
		
//		refundID.getText().setText(UUID.GenUUID15());
		refundID.getText().setEditable(false);
		TOrder.getText().setEditable(false);
		happenTime.getText().setEditable(false);
		reason.getText().setEditable(false);
		status.getBox().setEnabled(false);
		replyTime.getText().setEditable(false);
		TUser.getText().setEditable(false);
		
		main_jp.add(refundID);
		main_jp.add(TOrder);
		main_jp.add(happenTime);
		main_jp.add(reason);
		main_jp.add(replyTime);
		main_jp.add(status);
//		main_jp.add(manager);
		main_jp.add(TUser);
		
		JPanel p1 = new JPanel();
		agreeButton=new JButton("同意退款(Ctrl+S)");
		disagreeButton=new JButton("拒绝退款(Ctrl+D)");
		cancelButton=new JButton("取消(Ctrl+Q)");
		p1.add(agreeButton);
		p1.add(disagreeButton);
		p1.add(cancelButton);
		main_jp.add(p1);
		this.add(main_jp);
	}

	private void initListener(){
		agreeButton.addActionListener(this);
		agreeButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		disagreeButton.addActionListener(this);
		disagreeButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_D, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		cancelButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == agreeButton) {
					tOrderRefund.setStatus((byte)2);
					tUser = tOrderRefund.getTUser();
					tUser.setMoney(tUser.getMoney()+tOrderRefund.getTOrder().getPayedAmount());
			try {
				this.tUserService.saveOrUpdateService(tUser);
				this.tOrderRefundService.saveOrUpdateService(tOrderRefund);
				this.isSuccess=true;
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("处理退款信息");	
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if(e.getSource() == disagreeButton) {
					tOrderRefund.setStatus((byte)3);
			try {
				this.tOrderRefundService.saveOrUpdateService(tOrderRefund);
				this.isSuccess=true;
					BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("处理退款信息");	
				JOptionPane.showMessageDialog(null, "操作成功");
			} catch (Exception e1) {
				e1.printStackTrace();
				this.isSuccess=false;
				JOptionPane.showMessageDialog(null, "操作失败");
			}
			this.dispose();
		} else if(e.getSource() == cancelButton) {
			this.isSuccess=false;
			this.dispose();
		}
	}
	
	public void showDialog(TOrderRefund tOrderRefund){
		this.isSuccess=false;
		if(tOrderRefund!=null){
			this.tOrderRefund=tOrderRefund;
			refundID.getText().setText(tOrderRefund.getRefundID());
			TOrder.getText().setText(tOrderRefund.getTOrder().getOrderID());
			String happenDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(tOrderRefund.getHappenTime());
			happenTime.getText().setText(happenDate);
			reason.getText().setText(tOrderRefund.getReason());
			status.getBox().setSelectedIndex(tOrderRefund.getStatus()-1);
			String replyDate = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(tOrderRefund.getReplyTime());
			replyTime.getText().setText(replyDate);
			TUser.getText().setText(tOrderRefund.getTUser().getRealName());
			if(tOrderRefund.getStatus()!=1){
				agreeButton.setEnabled(false);
				disagreeButton.setEnabled(false);
			}
		}
		this.setVisible(true);
	}

}
